<template>
	<div class="partnerList">
		<div class="partnerList_top">
			<el-breadcrumb separator-class="el-icon-arrow-right" style="margin-top: 10px;">
				<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
				<el-breadcrumb-item>商品管理</el-breadcrumb-item>
				<el-breadcrumb-item>拼团商品列表</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="partnerList_bottom">
			<div class="partnerList_bottom_new">
				<div class="partnerList_bottom_new_goods">
					<div class="partnerList_bottom_new_goods_screening">
						<span>卖场：</span>
						<el-select
						  v-model="market_id"
						  clearable
						  @change="selectMarket"
						  filterable
						  placeholder="请选择卖场"
						>
						  <el-option
						    v-for="item in marketList"
						    :key="item.id"
						    :label="item.name"
						    :value="item.id"
						  >
						  </el-option>
						</el-select>
						<span>商户：</span>
							<el-select v-model="store_id" filterable clearable placeholder="选择商户" @change="selectStore">
								<el-option v-for="item in storeList" :key="item.id" :label="item.store_name"
									:value="item.id">
								</el-option>
							</el-select>
						<div class="item">
							<el-input v-model="keyword" placeholder="搜索"></el-input>
						</div>
						<div class="item">
							<button @click="handerSeach" class="searchBtn"><i class="el-icon-search"></i>搜索</button>
						</div>
					</div>
				</div>
			</div>
			<div class="partnerList_table">
				<div class="partnerList_bottom_table">
					<table>
						<tr>
							<th></th>
							<th>序号</th>
							<th>商户名称</th>
							<th>商品图</th>
							<th>商品名称</th>
							<th>拼团价格</th>
							<th>原价</th>
							<th>开始时间</th>
							<th>结束时间</th>
							<th>是否上架</th>
							<th>排序</th>
							<!-- <th>操作</th> -->
						</tr>
						<tr v-for="(v,i) in listData" :key="i">
							<td>
								<font class="checkbox" :class="{'check_active' : checkBoxAll.includes(i)}"
									@click="handlerCheckbox(i)"><i class="el-icon-check"></i></font>
							</td>
							<td>{{v.product_id}}</td>
							<td>{{v.store_name}}</td>
							<td>
								<div style="width: 80px;height: 80px;overflow: hidden;margin:4px auto;">
									<img :src="v.main_imgurl" style="width: 100%;height: 100%;">
								</div>
							</td>
							<td>
								{{v.product_title}}
							</td>
							<td>
								{{v.activity_price}}
							</td>
							<td>
								{{v.primary_price}}
							</td>
							<td>
								{{v.activity_start_at?formatDate(v.activity_start_at):'---'}}
							</td>
							<td>
								{{v.activity_end_at?formatDate(v.activity_end_at):'---'}}
							</td>
							<td>
								{{v.status==1?'上架':'下架'}}
							</td>
							<td>
							  <el-input
							    style="width: 80px; margin-right: 10px"
							    size="mini"
							    v-model="v.sort"
							  ></el-input>
							  <el-button
							    type="primary"
							    style="width: 60px; text-align: center"
							    size="mini"
							    @click="changeSort(v.product_id, v.sort)"
							    >确定</el-button
							  >
							</td>
						</tr>
						<tr v-if="listData.length == 0">
							<td class="table_title" colspan="10">当前数据空空如也</td>
						</tr>
					</table>
					<div class="partnerList_bottom_page">
						<div class="partnerList_bottom_page_left">
							<div class="partnerList_bottom_page_left_checkAll" @click="handerCheckAll">
								<font class="checkbox" :class="{'check_active' : checkAll}"><i
										class="el-icon-check"></i></font>
							</div>全选
							<!-- <button class="partnerList_bottom_page_left_btn" @click="selectDel">删除</button> -->
						</div>
						<div class="partnerList_bottom_page_right">
							<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
								:current-page="page" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize"
								layout="total, sizes, prev, pager, next, jumper" :total="total" background>
							</el-pagination>
						</div>
					</div>
				</div>
			</div>
			
		</div>
	</div>
</template>

<script>
	import modal from 'ant-design-vue/lib/modal';
	import 'ant-design-vue/lib/table/style/css'; // 或者 ant-design-vue/lib/button/style/css 加载 css 文件
	import 'ant-design-vue/lib/modal/style/css'; // 或者 ant-design-vue/lib/button/style/css 加载 css 文件
	export default {
		components: {
			modal
		},
		data() {
			return {
				listData: [],
				keyword: '',
				cate: {},
				input: '',
				checkAll: false,
				page: 1,
				pageSize: 10,
				total: 0,
				checkBoxAll: [], // 选中的数据
				shelvesAlls: [],
				tableList: [],
				status: 1, //团队状态
				visibleShow: false,
				merchantShow: false, //编辑窗口显示
				dialogVisible: false,
				dialogImageUrl: "", //图片
				fileList: [],
				title: '', //团队标题
				sub_title: '', //团队副标题
				statusList: [{
					value: 1,
					label: '正常'
				}, {
					value: 2,
					label: '隐藏'
				}],
				MadalTitle: '',
				activity_id: '', //活动id
				market_id: '', //卖场
				marketList: [], //卖场列表
				activityKeyword: '', //活动搜索
				storeList: [], //商户列表
				store_id: '', //选择的商户
				team_id: '', //团队id
				edit: false, //是否编辑
			}
		},
		mounted(options) {
			let data = this.$route.query;
			//团队商户列表
			this.getList();
			this.remoteMethod();
			//获取商户列表
			this.getStoreList();
		},

		methods: {
			// 获取卖场列表
			remoteMethod() {
			  this.$http.post("/api/root/Market/marketList").then((res) => {
			    if (res.code == 1) {
			      this.marketList = res.data ? res.data : [];
			    }
			  });
			},
			// 选择卖场
			selectMarket() {
			  this.getList();
			},
			//选择商户
			selectStore(){
				this.getList();
			},
			//获取拼团商品列表
			getList() {
				let params = {
					keyword: this.keyword,
					market_id: this.market_id,
					store_id:this.store_id,
					page: this.page,
					pagesize: this.pageSize,
				}
				let that = this;
				that.$http.post('/api/root/pintuan/product/lists', params).then(res => {
					if (res.code === 1) {
						var data = res.data.products
						if (data.length > 0) {
							that.total = res.data.total
							that.listData = data
						} else {
							that.total = 0
							that.listData = []
							that.$message.error('暂无记录')
						}
					} else {
						that.$message.error(res.msg)
					}
				}).catch(err => {
					that.$message.error(err.msg ? err.msg : err.toString())
				})
			},
			//获取商户列表
			getStoreList(){
				let params = {
					entry_status: 1,
					delete:0,
				}
				let that = this;
				that.$http.post('/api/root/store/select/all', params).then(res => {
					if (res.code === 1) {
						let data = res.data
						if (data.length > 0 || res.code===1) {
							that.storeList = data
							
						} else {
							that.storeList = []
						}
					} else {
						that.$message.error(res.msg)
					}
				}).catch(err => {
					that.$message.error(err.msg ? err.msg : err.toString())
				})
			},
			// 图片上传
			handleRemove(file, fileList) {
				this.fileList = fileList;
			},
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url;

				this.dialogVisible = true;
			},
			handleExceed(files, fileList) {
				this.$message.warning(`当前限制上传一张图片,请先删除原图片`);
			},
			handleAvatarSuccess(res, file, fileList) {
				this.fileList = fileList;
				// console.log(this.fileList, "上传图片");
			},
			beforeAvatarUpload(file) {
				const isLt2M = file.size / 1024 / 1024 < 2;
				if (!isLt2M) {
					this.$message.error("上传商品图片大小不能超过 2MB!");
				}
				return isLt2M;
			},
			getHMS(time) {
				const hour = parseInt(time / 3600) < 10 ? '0' + parseInt(time / 3600) : parseInt(time / 3600)
				const min = parseInt(time % 3600 / 60) < 10 ? '0' + parseInt(time % 3600 / 60) : parseInt(time % 3600 / 60)
				const sec = parseInt(time % 3600 % 60) < 10 ? '0' + parseInt(time % 3600 % 60) : parseInt(time % 3600 % 60)
				return hour + ':' + min + ':' + sec
			},
			// 排序按钮
			changeSort(id, sort) {
			  console.log(id, sort, "=====");
			  this.$http
			    .post("/api/root/ProductShop/setProductSort", { id, sort })
			    .then((res) => {
			      console.log(res, "排序结果");
			      if (res.code == 1) {
			        this.$message.success("操作成功");
			        this.getList();
			      } else {
			        this.$message.warning(res.msg);
			      }
			    });
			},
			handleCancel() {
				this.MadalTitle = ''
				this.dialogVisible = false
				this.visibleShow = false
			},
			//隐藏弹窗
			storeCancel() {
				this.MadalTitle = ''
				this.merchantShow = false
			},
			//删除商户
			toDelete(id) {
				this.$confirm('是否移除该商户?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					let params = {
						id: id
					}
					let that = this;
					that.$http.post('/api/root/activity/team/store/del', params).then(res => {
						if (res.code === 1) {
							that.getList();
							that.$message.success('删除成功')
						} else {
							that.$message.error(res.msg)
						}
					}).catch(err => {
						that.$message.error(err.msg)
					})
				}).catch(() => {
					//取消
				});
			},
			// 选择状态
			selectStatus() {
				this.page = 1
				this.getShareList(this.room_id)
			},
			// 多选
			handlerCheckbox(i) {
				console.log(i);
				let arr = this.checkBoxAll;
				if (arr.includes(i)) {
					let index = arr.indexOf(i);
					if (index != -1) {
						arr.splice(index, 1);
						this.shelvesAlls.splice(index, 1);
						if (this.checkBoxAll.length !== this.tableList.length) {
							this.checkAll = false;
						}
					}
				} else {
					this.checkBoxAll.push(i);
					this.shelvesAlls.push(this.tableList[i].id);
					if (this.checkBoxAll.length == this.tableList.length) {
						this.checkAll = true;
					}
				};
				console.log(this.shelvesAlls);
			},
			// 全选与全不选
			handerCheckAll() {
				this.checkAll = !this.checkAll;
				let arr = this.tableList.length;
				this.checkBoxAll = [];
				this.shelvesAlls = [];
				if (this.checkAll) {
					for (let i = 0; i < arr; i++) {
						this.checkBoxAll.push(i);
					}
					this.checkBoxAll.map(v => {
						this.shelvesAlls.push(this.tableList[v].id.toString());;
					})
				} else {
					this.checkBoxAll = [];
					this.shelvesAlls = []
				}
				console.log(this.shelvesAlls);
			},
			//搜索
			handerSeach() {
              this.getList();
			},

			handleSizeChange(val) {
				this.pageSize = val;
				this.getShareList(this.room_id)
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getList(this.room_id)
			},
			formatDate(value) {
				let date = new Date(parseInt(value) * 1000);
				let y = date.getFullYear();
				let m = date.getMonth() + 1;
				m = m < 10 ? "0" + m : m;
				let d = date.getDate();
				d = d < 10 ? "0" + d : d;
				let h = date.getHours();
				h = h < 10 ? "0" + h : h;
				let minute = date.getMinutes();
				let second = date.getSeconds();
				minute = minute < 10 ? "0" + minute : minute;
				second = second < 10 ? "0" + second : second;
				return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
			},
		}
	}
</script>

<style lang="less" scoped>
	@import 'index.less';

	.partnerList_bottom_new_goods_screening {
		width: 100%;
		height: auto;
		display: flex;

		.searchBtn {
			max-width: 90px;
			min-width: 68px;
			height: 42px;
			line-height: 42px;
			background-color: #409EFF;
			margin-left: 5px;
			border: 0 none;
			outline: none;
			border-radius: 4px;
			color: #fff;
			cursor: pointer;
		}

		.inputBox {
			margin-right: 20px;
		}
	}
</style>
